package com.example.day04.controller;

import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.connector.kafka.source.KafkaSource;
import org.apache.flink.connector.kafka.source.enumerator.initializer.OffsetsInitializer;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class SourceKafkaOne {

    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

//        KafkaSource 设置为以流模式运行，因此作业永远不会停止，直到 Flink 作业失败或被取消
        KafkaSource<String> source = KafkaSource.<String>builder()
        .setBootstrapServers("node3:9092")
        .setGroupId("my-group")
        .setTopics("test-topic")
        .setStartingOffsets(OffsetsInitializer.latest()) // 来指定从不同的偏移量开始消费
        .setValueOnlyDeserializer(new SimpleStringSchema()) // 如何解析 Kafka 消息体中的二进制数据。
                .build();
        env.fromSource(source, WatermarkStrategy.noWatermarks(), "Kafka Source").print();

        env.execute("kafka source");

    }
}
